package com.googlecode.mvcguice.tag;

import groovy.lang.Closure;

import java.io.PrintWriter;
import java.text.DecimalFormat;
import java.util.Map;

public class CoreTags {
	@SuppressWarnings("unchecked")
	public void foreach(Map<String, Object> args, Closure closure, PrintWriter out) {
		final Iterable<Object> iterable = (Iterable<Object>) args.get("iterable");
		for (Object element : iterable) {
			out.append(closure.call(new Object[] { element }).toString());
		}
	}

	public void formatDecimal(Map<String, Object> args, PrintWriter out) {
		final Object value = args.get("value");
		final String format = (String) args.get("format");

		DecimalFormat formatter = new DecimalFormat(format);

		out.print(formatter.format(value));
	}
}
